/**
 * @Title:  MyEditText.java 
 * @Copyright:  XXX Co., Ltd. Copyright YYYY-YYYY,  All rights reserved 
 * @Description:  TODO<请描述此文件是做什么的> 
 * @author:  MeiYi 
 * @data:  2016-6-1 上午11:37:32 
 * @version:  V1.0 
 */

package artid.com.arttogo.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

import artid.com.arttogo.R;
import artid.com.arttogo.utils.NetUtils;


/**
 * TODO 右方有清除文本按钮的EditText
 * 
 * @author 熊彬
 * @data: 2016-6-1 上午11:37:32
 * @version: V1.0
 */
public class MyEditText extends EditText {
	@SuppressWarnings("unused")
	private Context context;
	private Drawable mIconSearchClear; // 搜索文本框清除文本内容图标

	/**
	 * @param context
	 */
	public MyEditText(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.context = context;
		initSearchClear();
	}

	/**
	 * @param context
	 * @param attrs
	 */
	public MyEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.context = context;
		initSearchClear();
	}

	/**
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public MyEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		this.context = context;
		initSearchClear();
	}

	/**
	 * 
	 */
	@SuppressLint("ClickableViewAccessibility")
	private void initSearchClear() {
		// TODO Auto-generated method stub
		Resources res = getResources();
		mIconSearchClear = res.getDrawable(R.drawable.cancel_icon);
		addTextChangedListener(tbxSearch_TextChanged);
		setOnTouchListener(txtSearch_OnTouch);
		setOnFocusChangeListener(mOnFocusChangeListener);
	}

	private OnFocusChangeListener mOnFocusChangeListener = new OnFocusChangeListener() {

		@Override
		public void onFocusChange(View arg0, boolean hasFocus) {
			// TODO Auto-generated method stub
			if (hasFocus) {
				// 此处为得到焦点时的处理内容
				if (!TextUtils.isEmpty(getText().toString())) {
					setCompoundDrawablesWithIntrinsicBounds(null, null,
							mIconSearchClear, null);
				}
			} else {
				// 此处为失去焦点时的处理内容
				setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
			}
		}
	};
	/**
	 * 监听图片区域点击事件
	 */
	private OnTouchListener txtSearch_OnTouch = new OnTouchListener() {
		@SuppressLint("ClickableViewAccessibility")
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			switch (event.getAction()) {

			case MotionEvent.ACTION_UP:
				int curX = (int) event.getX();
				if (curX > v.getWidth() - NetUtils.dip2px(context, 25)
						&& !TextUtils.isEmpty(getText())) {
					setText("");
					setHint("");
					int cacheInputType = getInputType();// backup
														// the input
														// type
					setInputType(InputType.TYPE_NULL);// disable
														// soft
														// input
					onTouchEvent(event);// call native handler
					setInputType(cacheInputType);// restore input
													// type
					return true;// consume touch even
				}
				break;
			}
			return false;
		}
	};
	/**
	 * 动态搜索
	 */
	private TextWatcher tbxSearch_TextChanged = new TextWatcher() {

		// 缓存上一次文本框内是否为空
		private boolean isnull = true;

		@Override
		public void afterTextChanged(Editable s) {
			if (TextUtils.isEmpty(s)) {
				if (!isnull) {
					setCompoundDrawablesWithIntrinsicBounds(null, null, null,
							null);
					isnull = true;
				}
			} else {
				if (isnull) {
					setCompoundDrawablesWithIntrinsicBounds(null, null,
							mIconSearchClear, null);
					isnull = false;
				}
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
									  int after) {
		}

		/**
		 * 随着文本框内容改变动态改变列表内容
		 */
		@Override
		public void onTextChanged(CharSequence s, int start, int before,
								  int count) {

		}
	};
}
